package morphia.spring;

import org.springframework.util.StringUtils;

/**
 * {@link DbDefinition} implementation.
 * @author Kent Yeh
 */
public class DbDefinitionImpl implements DbDefinition {

    private String host = "localhost";
    private int port = 27017;
    private String optionsRef = null;
    private String databaseName = null;
    private String username = null;
    private String password = null;

    public static String getDefaultDefName(DBDef dbdef) {
        return StringUtils.hasText(dbdef.name()) ? dbdef.name()
                : new StringBuilder(dbdef.host()).append('_').append(dbdef.port()).append('_').append(dbdef.databaseName()).toString();
    }

    public DbDefinitionImpl(DBDef dbdef) {
        this.host = dbdef.host();
        this.port = dbdef.port();
        this.databaseName = dbdef.databaseName();
        this.optionsRef = dbdef.optionsRef();
        this.username = dbdef.username();
        this.password = dbdef.password();
    }

    public DbDefinitionImpl(String host, int port, String databaseName) {
        this.host = host;
        this.port = port;
        this.databaseName = databaseName;
    }

    @Override
    public String getDatabaseName() {
        return databaseName;
    }

    @Override
    public String getHost() {
        return host;
    }

    @Override
    public String getOptionsRef() {
        return StringUtils.hasText(optionsRef) ? optionsRef : null;
    }

    public void setOptionsRef(String optionsRef) {
        this.optionsRef = optionsRef;
    }

    @Override
    public String getPassword() {
        return StringUtils.hasText(password) ? password : null;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public int getPort() {
        return port;
    }

    @Override
    public String getUsername() {
        return StringUtils.hasText(username) ? username : null;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final DbDefinitionImpl other = (DbDefinitionImpl) obj;
        if ((this.host == null) ? (other.host != null) : !this.host.equals(other.host)) {
            return false;
        }
        if (this.port != other.port) {
            return false;
        }
        if ((this.databaseName == null) ? (other.databaseName != null) : !this.databaseName.equals(other.databaseName)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + (this.host != null ? this.host.hashCode() : 0);
        hash = 53 * hash + this.port;
        hash = 53 * hash + (this.databaseName != null ? this.databaseName.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return new StringBuilder(databaseName).append('@').append(host).append(':').append(port).toString();
    }
}
